new Vue({
    el: '#app',
    data() {
        return {
            Purchase_Order_SerialNumber: "",
            Wares: [],
            formAcceptance: {
                Children: {
                    purchase_order_detail: []
                }
            },
            formAcceptance1: [],
            options: [
                {
                    label: '仓库',
                    value: 0
                },
                // {
                //   label: '店铺',
                //   value: 1,
                // }
            ],
            formware: {
                Purchase_Receipt_SerialNumber: "",
                Receipt_Date: "",
                Receipt_Type: 0,
                Store_SerialNumber: "",
                Children: {
                    purchase_receipt_detail: [{
                        Purchase_Receipt_Detail_SerialNumber: "",
                        Purchase_Receipt_SerialNumber: "",
                        Purchase_Order_SerialNumber: "",
                        Article_SerialNumber: "",//商品建
                        Receipt_Quantity: "",//入库数量
                        Remain_Quantity: "",//剩余数量
                        Out_Total_Quantity: "",//出库数量
                        Amount: "",//价格
                        Expiration_Date: "",//过期时间
                    }]
                }
            },
            ArticleAndBrand: [],
            rules: {
                Store_SerialNumber: [{ required: true, message: '请选择仓库', trigger: 'change' }],
                Receipt_Type: [{ required: true, message: '请选择存货地点', trigger: 'change' }],
            },
            formData: [],
            loading: false,
        }
    },
    mounted() {
        this.Purchase_Order_SerialNumber = sessionStorage.getItem('Purchase_Order_SerialNumber')
        this.getWares()
        this.GetArticleAndBrand(this.Purchase_Order_SerialNumber, () => {
            this.getGoods(this.Purchase_Order_SerialNumber)
        })
    },
    computed: {
        ArticleName() {
            return function (id) {
                let str = ''
                if (id) {
                    if (this.ArticleAndBrand.some(item => item.Purchase_Order_Detail_SerialNumber == id)) {
                        let obj = this.ArticleAndBrand.filter(item => item.Purchase_Order_Detail_SerialNumber == id)[0]
                        return obj.Article_Name
                    }
                    return str
                } else {
                    return str
                }
            }
        },
        Brand_Name() {
            return function (id) {
                if (id) {
                    let obj = this.ArticleAndBrand.filter(item => item.Purchase_Order_Detail_SerialNumber == id)[0]
                    return obj.Brand_Name
                } else {
                    return ''
                }
            }
        },
        Warehouse_Name() {
            return function (id) {
                if (id) {
                    let obj = this.ArticleAndBrand.filter(item => item.Purchase_Order_Detail_SerialNumber == id)[0]
                    return obj.Warehouse_Name
                } else {
                    return ''
                }
            }
        }
    },
    methods: {
        getWares() {//获取所有可选仓库
            let this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: 'warehouse_info',
                },
                success: function (res) {
                    res.forEach(item => {
                        this_.Wares.push({
                            label: item.Warehouse_Name,
                            value: item.Applicable_Type == '0' ? item.Warehouse_SerialNumber : item.Parent_Warehouse_SerialNumber
                        })
                    })
                    console.log(this_.Wares)
                },
                dataType: "json"
            });
        },
        ChangeNum(e) {
            console.log(e)
            this.formware.Children.purchase_receipt_detail[0].Receipt_Quantity = this.formAcceptance.Children.purchase_order_detail[0].Receipt_Quantity
        },
        getGoods(Purchase_Order_SerialNumber) {//获取所选商品
            let this_ = this
            $.ajax({
                url: DomainName + '/getSubtableInquiryOriginal?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Primary_Key: Purchase_Order_SerialNumber,
                    JSONObject: JSON.stringify({ purchase_order: { purchase_order_detail: {} } }),
                },
                success: function (res) {
                    console.log(res)
                    console.log(this_.ArticleAndBrand);
                    if (res.purchase_order.length == 1) {
                        this_.formAcceptance = res.purchase_order[0]
                        this_.formware.Children.purchase_receipt_detail = []
                        this_.formware.Store_SerialNumber = res.purchase_order[0].Children.purchase_order_detail[0].Store_SerialNumber
                        this_.formAcceptance.Children.purchase_order_detail.forEach(item => {
                            item.Receipt_Quantity = item.Quantity
                            this_.formware.Children.purchase_receipt_detail.push({
                                Purchase_Order_Detail_SerialNumber: item.Purchase_Order_Detail_SerialNumber,
                                Purchase_Receipt_Detail_SerialNumber: "",
                                Purchase_Receipt_SerialNumber: "",
                                Purchase_Order_SerialNumber: item.Purchase_Order_SerialNumber,
                                Article_SerialNumber: item.Article_SerialNumber,
                                Receipt_Quantity: item.Receipt_Quantity,
                                Amount: item.Article_UnitPrice,
                                Expiration_Date:"",
                            })
                        })
                    }
                    console.log(this_.formware.Children.purchase_receipt_detail)
                },
                dataType: "json"
            });
        },
        ModifyGoods() {//修改商品
            console.log(this.formware.Children.purchase_receipt_detail)
            if(this.formware.Children.purchase_receipt_detail[0].Expiration_Date==''){
                this.$message({
                    message: '请填写过期时间',
                    type: 'error'
                })
                return
            }
            let this_ = this
            this.loading = true
            this.formAcceptance.Row_Status = 6
            this.formAcceptance.Children.purchase_order_detail.forEach(item => {
                item.Not_Receipt_Quantity = item.Quantity - item.Receipt_Quantity
            })
            let data = {
                Data: JSON.stringify(
                    {
                        Modify: { 'purchase_order': [this_.formAcceptance] },
                    }
                )
            }
            $.ajax({
                url: DomainName + '/getEditAction?token=' + $.cookie("token"),
                type: 'POST',
                data,
                success: function (res) {
                    console.log(res)
                    if (res.Status == 'success') {
                        this_.Sumbit()
                    }
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        back() {
            window.history.go(-1)
        },
        GetInquiry(Primary_Key, i) {//查原值
            let this_ = this
            $.ajax({
                url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Primary_Key_Name: 'Purchase_Order_SerialNumber',
                    Primary_Key,
                    Data: 'purchase_order',
                },
                success: function (res) {
                    console.log(res)
                    this_.formData = res[0]
                },
                dataType: "json"
            });
        },
        Sumbit() {// 入库
            let this_ = this
            this_.loading = true
            this.formware.Receipt_Date = getYMDHMSDate()
            this.formware.Enterprise_Info_SerialNumber = $.cookie("enterprise")
            this.formware.Children.purchase_receipt_detail[0].Remain_Quantity = this.formware.Children.purchase_receipt_detail[0].Receipt_Quantity
            this.formware.Children.purchase_receipt_detail[0].Out_Total_Quantity = 0
            console.log(this.formware)
            $.ajax({
                url: DomainName + '/setInventoryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: JSON.stringify({ 'purchase_receipt': [this_.formware] })
                },
                success: function (res) {
                    console.log(res)
                    this_.$message({
                        message: '验收入库成功',
                        type: 'success'
                    })
                    this_.back()
                    this_.loading = false
                },
                dataType: "json"
            });
        },
        GetArticleAndBrand(Purchase_Order_SerialNumber, Callback) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getPurchaseOrderDetailIDAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Purchase_Order_SerialNumber,
                },
                success: function (res) {
                    console.log(res)
                    this_.ArticleAndBrand = res.Data
                    Callback()
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
        },
        Pricethousands(num) {//转千分位
            return thousands(num)
        },
    },
})